/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.wisnu.spms.source.controller;

import com.wisnu.spms.source.model.PanelPaymentModel;
import com.wisnu.spms.source.view.PanelPayment;
import javax.swing.JOptionPane;

/**
 *
 * @author Wisnu Wardoyo
 */
public class PanelPaymentController {

    private final PanelPaymentModel model;

    public PanelPaymentController(PanelPaymentModel model) {
        this.model = model;
    }

    public void getBillingAmount(int order) {
        try {
            model.setBillingAmount(order);
        } catch (Exception ex) {
            JOptionPane.showMessageDialog(null, "Tidak dapat menampilkan catatan pembayaran");
        }
    }

    public void initPage(Integer id) {
        try {
            if (model.checkStudent(id) == 1) {
                model.setId(id);
                model.loadPage();
            } else {
                JOptionPane.showMessageDialog(null, "Siswa Dengan ID " + id + " Tidak Ditemukan!");
            }
        } catch (Exception ex) {
            JOptionPane.showMessageDialog(null, ex.getMessage());
        }
    }

    public void recordPayment(PanelPayment panelPayment) {
        if (panelPayment.getComBilling().getSelectedIndex() != 0) {
            Integer order = panelPayment.getComBilling().getSelectedIndex() - 1;
            if (JOptionPane.showConfirmDialog(panelPayment,
                    new Object[]{"Apakah anda akan membayar : ",
                        panelPayment.getComBilling().getSelectedItem(),
                        "Sejumlah : Rp." + panelPayment.getTxtBillingAmount().getText() + " ?"},
                    "Konfirmasi Pemayaran", JOptionPane.YES_NO_OPTION, 
                    JOptionPane.QUESTION_MESSAGE) == JOptionPane.YES_OPTION) {
                try {
                    model.recordPayment(order);
                    model.billingChecker(order);
                } catch (Exception ex) {

                } finally {
                    try {
                        model.setBillingAmount(order);
                    } catch (Exception ex) {

                    }
                }
            }
        }
    }
}
